home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2179 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  61 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help: floating point/use of floor
  5. Date: 19 Jan 1996 17:00:52 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan19120052@g7240065.bridge.bst.bls.com>
  8. References: <4dogri$fta@gate.service.britgas.co.uk>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: andy.crutchlow@service.britgas.co.uk's message of 19 Jan 1996
  11.     16:28:34 GMT
  12.  
  13. In article <4dogri$fta@gate.service.britgas.co.uk> andy.crutchlow@service.britgas.co.uk (Andy Crutchlow) writes:
  14.  
  15. : Can anyone help.
  16.  
  17. : Make the following display 75.99
  18. : main()
  19. : {
  20. : int i;
  21. : float f;
  22. : i = 7599;
  23. : f = i / 100.00;
  24. : printf("%f\n",f);
  25. : }
  26.  
  27. The problem with floats (and doubles) is that some numbers are not
  28. directly representable.
  29. What may not be directly representable in floats may be representable in
  30. doubles.
  31. I can make it print 75.99 but I can't guarentee yours will do the same.
  32.  
  33. #include <stdio.h>
  34.  
  35. int
  36. main(void)
  37. {
  38.     float f = 75.99;
  39.     double d = 75.99;
  40.     printf("%f\n", f);      /* prints 75.989998 */
  41.     printf("%.2f\n", f);    /* prints 75.99 */
  42.     printf("%g\n", f);      /* prints 75.99 */
  43.     printf("%e\n", f);      /* prints 7.599000e+01 */
  44.  
  45.     printf("%f\n", d);      /* prints 75.990000 */
  46.     printf("%g\n", d);      /* prints 75.99 */
  47.     printf("%e\n", d);      /* prints 7.599 */
  48. }
  49.  
  50. : I believe the correct answer involves using function floor but I can't
  51. : remember how.
  52.  
  53. floor() will not help - it returns the largest integer not greater than
  54. its argument.
  55.  
  56. Regards
  57.  
  58.    -A.
  59. -- 
  60. | A.Champion                |
  61.